package com.ysd.lis.controller.callNum;


import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ysd.common.Result;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.entity.callNum.CallBusiness;
import com.ysd.lis.entity.callNum.CallWindow;
import com.ysd.lis.service.callNum.CallBusinessService;
import com.ysd.lis.service.callNum.CallWindowService;
import com.ysd.lis.socket.MessageTemplate;
import com.ysd.lis.socket.WebSocketUtils;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;

/**
 * <p>
 * 抗生素 前端控制器
 * </p>
 *
 * @author pc
 * @since 2024-01-11
 */
@RestController
@RequestMapping("/callNum")
public class CallNumController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(CallNumController.class);

    @Autowired
    CallWindowService callWindowService;
    @Autowired
    CallBusinessService callBusinessService;

    @ApiOperation("取号websocket传给前端")
    @PostMapping("/addCallNum")
    public Result addCallNum(@RequestBody JSONObject jsonObject) {
        MessageTemplate messageTemplate = new MessageTemplate();
        messageTemplate.setMessage(JSONUtil.toJsonStr(jsonObject));
        messageTemplate.setType("getNum");
        WebSocketUtils.sendAllMessage(messageTemplate);
        return Result.succ(1, "", "取号成功");
    }

    @GetMapping("/testThreadNum")
    public void testThreadNum() throws InterruptedException {
        System.out.println(Thread.currentThread().getName());
    }

    @GetMapping("/bigScreenGetWindow")
    public Result bigScreenGetWindow(String currentWindow) {
        LambdaQueryWrapper<CallWindow> lambdaQueryWrapper = new LambdaQueryWrapper<CallWindow>();
        lambdaQueryWrapper.eq(CallWindow::getDelFlag, 0);
        lambdaQueryWrapper.eq(StrUtil.isNotEmpty(currentWindow), CallWindow::getWinCode, currentWindow);
        List<CallWindow> list = callWindowService.list(lambdaQueryWrapper);
        return Result.succ(1, "查询成功", list);
    }

    @GetMapping("/bigScreenGetBusiness")
    public Result bigScreenGetBusiness(String currentWindow, String state) {
        LambdaQueryWrapper<CallBusiness> lambdaQueryWrapper = new LambdaQueryWrapper<CallBusiness>();
        lambdaQueryWrapper.eq(CallBusiness::getDelFlag, 0);
        lambdaQueryWrapper.eq(StrUtil.isNotEmpty(state), CallBusiness::getState, state);
        lambdaQueryWrapper.eq(StrUtil.isNotEmpty(currentWindow), CallBusiness::getWinCode, currentWindow);
        lambdaQueryWrapper.orderByAsc(CallBusiness::getReqTime);
        List<CallBusiness> list = callBusinessService.list(lambdaQueryWrapper);
        return Result.succ(1, "查询成功", list);
    }

    //护士叫号改状态，并向大屏发送消息处理
    @PostMapping("/nurseCallNum")
    public Result nurseCallNum(@RequestBody CallBusiness callBusiness) {
        MessageTemplate messageTemplate = new MessageTemplate();
        messageTemplate.setMessage(JSONUtil.toJsonStr(callBusiness));
        messageTemplate.setType("nurseCallNum");
        WebSocketUtils.sendAllMessage(messageTemplate);
        Timestamp reqTime = callBusiness.getReqTime();
        Timestamp nowTime = new Timestamp(System.currentTimeMillis());
        // nowTime-reqTime 算出分钟
        callBusiness.setAvgTime((int) (nowTime.getTime() - reqTime.getTime()) / 60000);
        boolean b = callBusinessService.saveOrUpdate(callBusiness);
        return Result.succ(1, "", "叫号成功");
    }
}
